package com.hudongwx.shansun.define;

import android.content.Context;
import android.icu.text.DisplayContext;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.hudongwx.shansun.R;

/**
 * Created by hudongwx on 16-10-28.
 */
public class MyPwdView extends FrameLayout {
    private ImageView iv1, iv2;
    private EditText et;

    public MyPwdView(Context context) {
        super(context);
        initView(context);
    }

    public MyPwdView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);

    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.mypwdinput, this, true);
        iv1 = (ImageView) view.findViewById(R.id.mypwd_in_iv_1);
        iv2 = (ImageView) view.findViewById(R.id.mypwd_in_iv_2);
        et = (EditText) view.findViewById(R.id.mypwd_in_et);

        iv1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iv1.isSelected()) {
                    iv1.setSelected(false);

                    et.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    iv1.setSelected(true);
                    et.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);


                }
            }
        });

        iv2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et.setText("");
            }
        });
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    iv2.setVisibility(View.GONE);
                } else {
                    iv2.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private EditText getEditText() {
        return et;
    }
}
